package io.renren.api;


import io.renren.utils.R;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/jssdk")
@Slf4j
public class ApiJssdkController {

    @Autowired
    private WxMpService wxMpService;

    /**
     * 根据url获取签名
     *
     * @param url
     * @return
     */
    @RequestMapping("/getBaseSignature")
    public R getBaseSignature(@RequestParam String url) throws WxErrorException {

        if (StringUtils.isEmpty(url)) {
            log.info("url 为空");
            return R.error(1, "url不能为空");
        }

        WxJsapiSignature jsapiSignature = wxMpService.createJsapiSignature(url);
        log.info("baseSignature ==> {}",jsapiSignature.toString());
        return R.ok().put("signData", jsapiSignature);

    }

}
